package com.Jano.dto;/**
 * @ClassName : LoginUser
 * @Description :
 * @Author : Jano
 * @Date: 2022-07-24 17:11
 */

import com.Jano.entity.User;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 1.1因为UserDetailsService方法的返回值是UserDetails类型，
 * 所以需要定义一个类，实现该接口，把用户信息封装在其中
 * @author zoumaoji
 * @date 2022/07/24 17:11
 **/
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {
    /**
     *自定义的User对象
     * @author zoumaoji
     * @date 2022/07/24 23:46
     * @param null
     * @return
     */
    private User user;

    /**
     * 用户的权限集合
     * @author zoumaoji
     * @date 2022/07/25 19:33
     * @param null
     * @return
     */
    private List<String> permissions;

    public LoginUser(User user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    /**
     * 权限集合 注意这里不讲权限信息序列化到redis中为了安全性
     * @author zoumaoji
     * @date 2022/07/25 19:42
     * @param null
     * @return
     */
    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority>  authorities;

    /**
     * 返回权限信息
     *
     * @return java.util.Collection<? extends org.springframework.security.core.GrantedAuthority>
     * @author zoumaoji
     * @date 2022/07/24 17:11
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities!=null){
            return authorities;
        }
        //把permissions中的权限信息封装成GrantedAuthority的一个实现类SimpleGrantedAuthority对象
        List<SimpleGrantedAuthority> collect = permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取用户密码
     *
     * @return java.lang.String
     * @author zoumaoji
     * @date 2022/07/24 17:21
     */
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    /**
     * 获取用户名
     *
     * @return java.lang.String
     * @author zoumaoji
     * @date 2022/07/24 17:21
     */
    @Override
    public String getUsername() {
        return user.getUserName();
    }

    /**
     * 是否未过期(暂时改为了true)
     *
     * @return boolean
     * @author zoumaoji
     * @date 2022/07/24 17:12
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否未被锁定
     *
     * @return boolean
     * @author zoumaoji
     * @date 2022/07/24 17:22
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /*
     *凭据是否未过期
     * @author zoumaoji
     * @date 2022/07/24 17:22
     * @return boolean
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /*
     *是否可用
     * @author zoumaoji
     * @date 2022/07/24 17:12
     * @return boolean
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
